//
//  HomeViewController.m
//  Geography
//
//  Created by RJET04 on 2023/8/21.
//

#import "HomeViewController.h"
#import "HomeCategoryCell.h"

@interface HomeViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

@property (nonatomic, strong) ZYView *topView;
@property (nonatomic, strong) ZYTextField *searchTf;
@property (nonatomic, strong) ZYLabel * searchBtn;
@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *btmv;

@property (nonatomic, strong) UIImageView *clientImageView;
@property (nonatomic, strong) UILabel *clientLab;
@property (nonatomic, strong) ZYLabel *switchLab;

@property (nonatomic, strong) ZYView *navView;
@property (nonatomic, strong) ZYLabel *titleLab;

@property (nonatomic, strong) NSString *env;

@property (nonatomic, strong) BaseJsonModel *model;

@property (nonatomic, strong) NSMutableArray *models;

@property (nonatomic, strong) PayView *payView;

@end

@implementation HomeViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationController.navigationBarHidden = YES;
    
    [self getAppStoreAppVersion:@"6463595356" jump:NO]; //获取app在appstore版本号
}

- (void)loadData:(NSString *)env
{
    //根据环境加载数据
    NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:env ofType:@"json"]];
    self.model = [BaseJsonModel yy_modelWithJSON:jsonData];
    
    if (self.models == NULL) {
        self.models = [NSMutableArray new];
    }else{
        [self.models removeAllObjects];
    }
    for (NSInteger i=0; i<self.model.textjson.index.list.count; i++) {
        IndexListModel *model = self.model.textjson.index.list[i];
        [self.models addObject:model];
    }

    self.title = self.model.textjson.index.title;
    self.switchLab.text = self.model.textjson.currency.edition;
    self.clientLab.text = self.model.textjson.currency.customer;
    self.titleLab.text = self.model.textjson.index.title;
    
    MJRefreshNormalHeader *header = (MJRefreshNormalHeader *)self.tableview.mj_header;

    if ([env isEqualToString:@"zh"]) {
        self.searchTf.placeholder = @"请输入搜索内容";
        self.searchBtn.text = @"搜 索";
        self.payView.titleLab.text = @"赞赏我们";
        self.payView.detailLab.text = @"给我们赞赏";
        self.payView.wxLab.text = @"微信赞赏";
        self.payView.zfbLab.text = @"支付宝赞赏";
        
        [header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
        [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
        [header setTitle:@"松开加载" forState:MJRefreshStatePulling];
    }else{
        self.searchTf.placeholder = @"Please enter contents";
        self.searchBtn.text = @"Search";
        self.payView.titleLab.text = @"Appreciate us";
        self.payView.detailLab.text = @"Give us appreciation";
        self.payView.wxLab.text = @"weixin pay";
        self.payView.zfbLab.text = @"alipay";
        
        [header setTitle:@"refreshing" forState:MJRefreshStateRefreshing];
        [header setTitle:@"Drop down to refresh" forState:MJRefreshStateIdle];
        [header setTitle:@"Release to refresh" forState:MJRefreshStatePulling];
    }
    [self.tableview reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.env = NSLocalizedString(@"env", @"");
    [self loadData:self.env];
    
    self.navigationController.navigationBar.barTintColor = THEME_COLOE;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [self setupViews];
    
    [self refresh];
}

- (void)setupViews
{
    self.navView = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(0, 0, SCREEN_WIDTH, kStatusBarHeight+kNavBarHeight))
        .backGroundColorBlock(THEME_COLOE);
    }];
    [self.view addSubview:self.navView];
    
    self.titleLab = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(0, kStatusBarHeight, SCREEN_WIDTH, kNavBarHeight))
        .textAlignmentBlock(NSTextAlignmentCenter)
        .fontBlock([UIFont systemFontOfSize:18])
        .textColorBlock([UIColor whiteColor])
        .textBlock(self.model.textjson.index.title)
        .backGroundColorBlock(THEME_COLOE);
    }];
    [self.navView addSubview:self.titleLab];
    
    CGFloat topH = 50;
    CGFloat padding = 5;
    self.topView = [ZYView createZYView:^(ZYView *view) {
        
        view.backGroundColorBlock([UIColor clearColor])
        .frameBlock(CGRectMake(0, 0, SCREEN_WIDTH, topH));
    }];
    self.searchBtn = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(SCREEN_WIDTH-12-100, padding, 100, topH-2*padding))
        .backGroundColorBlock(BTN_COLOE)
        .textColorBlock([UIColor whiteColor])
        .textBlock(NSLocalizedString(@"search", @""))
        .userEnableBlock(YES)
        .textAlignmentBlock(NSTextAlignmentCenter)
        .fontBlock([UIFont systemFontOfSize:15]);
        
        label.layer.cornerRadius = 5;
        label.layer.masksToBounds = YES;
    }];
    [self.topView addSubview:self.searchBtn];
    
    //MARK: -- 搜索
    __weak typeof(self) weakSelf = self;
    self.searchBtn.singalTapBlock = ^{
        
        [weakSelf search];
    };
    
    self.searchTf = [ZYTextField createZYTextField:^(ZYTextField *textField) {
        textField.frameBlock(CGRectMake(12, padding, CGRectGetMinX(self.searchBtn.frame)-12*2, topH-2*padding))
        .placeholderBlock(NSLocalizedString(@"placeholder", @""))
        .fontBlock([UIFont systemFontOfSize:15]);
        
        textField.layer.cornerRadius = 5;
        textField.layer.masksToBounds = YES;
        textField.layer.borderWidth = 1;
        textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    }];
    [self.topView addSubview:self.searchTf];
    self.searchTf.delegate = self;
    
    ZYView *lv = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(0, 0, 15, topH))
        .backGroundColorBlock([UIColor clearColor]);
    }];
    self.searchTf.leftView = lv;
    self.searchTf.leftViewMode = UITextFieldViewModeAlways;
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight+kNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-HOME_BOTTOM_H-kStatusBarHeight-kNavBarHeight)];
    self.tableview.backgroundColor = [UIColor clearColor];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.view addSubview:self.tableview];
    
    self.tableview.tableHeaderView = self.topView;

    ZYView *tbv = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(self.tableview.bounds);
    }];
    ZYImageView *tbimgv = [ZYImageView createZYImageView:^(ZYImageView *imageView) {
        imageView.frameBlock(CGRectMake(0, 0, tbv.frame.size.width, tbv.frame.size.height))
        .imageBlock([UIImage imageNamed:@"bj"]);
    }];
    [tbv addSubview:tbimgv];
    self.tableview.backgroundView = tbv;
    
    [self.tableview registerClass:[HomeCategoryCell class] forCellReuseIdentifier:NSStringFromClass([HomeCategoryCell class])];
    
    ZYView *btmv = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(0, SCREEN_HEIGHT-HOME_BOTTOM_H, SCREEN_WIDTH, HOME_BOTTOM_H))
        .backGroundColorBlock([UIColor whiteColor]);
    }];
    [self.view addSubview:btmv];
    self.btmv = btmv;
    
    ZYView *itembtmv = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(SCREEN_WIDTH/2-165/2, 0, 140, HOME_BOTTOM_H))
        .backGroundColorBlock([UIColor clearColor]);
    }];
    [btmv addSubview:itembtmv];
    self.bottomView = itembtmv;
    
    ZYLabel *clientLab = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(30, 0, 140, 60))
        .textColorBlock([UIColor blackColor])
        .textBlock(self.model.textjson.currency.customer)
        .fontBlock([UIFont boldSystemFontOfSize:16])
        .textAlignmentBlock(NSTextAlignmentLeft)
        .backGroundColorBlock([UIColor clearColor]);
    }];
    [self.bottomView addSubview:clientLab];
    self.clientLab = clientLab;
    
    self.clientImageView = [ZYImageView createZYImageView:^(ZYImageView *imageView) {
        imageView.frameBlock(CGRectMake(0, 30-25/2, 25, 25))
        .backGroundColorBlock([UIColor clearColor])
        .imageBlock([UIImage imageNamed:@"contact"]);
    }];
    [self.bottomView addSubview:self.clientImageView];
    
    ZYView *btmmaskv = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(0, SCREEN_HEIGHT-HOME_BOTTOM_H, SCREEN_WIDTH, HOME_BOTTOM_H))
        .backGroundColorBlock([UIColor clearColor]);
    }];
    [self.view addSubview:btmmaskv];
    
    //MARK: -- 客服
    btmmaskv.singalTapBlock = ^{
      
        UIAlertController *vc = [UIAlertController alertControllerWithTitle:[weakSelf.env isEqualToString:@"zh"] ? @"请选择" : @"Please choose " message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *a1 = [UIAlertAction actionWithTitle:[weakSelf.env isEqualToString:@"zh"] ? @"微信客服" : @"WeChat client service" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          
            if (![AlertUtils checkWxInstall:weakSelf env:weakSelf.env]) {
                return;
            }
            
            WXOpenCustomerServiceReq *serviceReq =[[WXOpenCustomerServiceReq alloc] init];
            serviceReq.corpid = @"wwd36f991b48fe4d3a";
            serviceReq.url = @"https://work.weixin.qq.com/kfid/kfc6c8c30baed785a35";
            [WXApi sendReq:serviceReq completion:^(BOOL success) {

                NSLog(@"接入微信客服结果: %d", success);
            }];
        }];
        UIAlertAction *a2 = [UIAlertAction actionWithTitle:[weakSelf.env isEqualToString:@"zh"] ? @"电话:073183030585" : @"tel://073183030585" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          
            NSMutableString * string = [[NSMutableString alloc] initWithFormat:@"tel:%@",@"073183030585"];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
        }];
        UIAlertAction *a3 = [UIAlertAction actionWithTitle:[weakSelf.env isEqualToString:@"zh"] ? @"取消" : @"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [vc addAction:a1];
        [vc addAction:a2];
        [vc addAction:a3];
        [self presentViewController:vc animated:YES completion:nil];
    };
    
    ZYLabel *switchLab = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(SCREEN_WIDTH-20-80, CGRectGetMinY(btmv.frame)-20-40, 80, 40))
        .textColorBlock([UIColor whiteColor])
        .textBlock(self.model.textjson.currency.edition)
        .textAlignmentBlock(NSTextAlignmentCenter)
        .userEnableBlock(YES)
        .backGroundColorBlock(BTN_COLOE);
        
        label.layer.cornerRadius = 5;
        label.layer.masksToBounds = YES;
    }];
    [self.view addSubview:switchLab];
    self.switchLab = switchLab;

    MJRefreshNormalHeader *header = (MJRefreshNormalHeader *)self.tableview.mj_header;
    
    if ([weakSelf.env isEqualToString:@"zh"]) {
        
        weakSelf.clientImageView.frame = CGRectMake(0, 30-25/2, 25, 25);
        weakSelf.clientLab.frame = CGRectMake(30, 0, 40, 60);
        [header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
        [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
        [header setTitle:@"松开加载" forState:MJRefreshStatePulling];
    }else{
        
        weakSelf.clientImageView.frame = CGRectMake(0, 30-25/2, 25, 25);
        weakSelf.clientLab.frame = CGRectMake(30, 0, 140, 60);
        [header setTitle:@"refreshing" forState:MJRefreshStateRefreshing];
        [header setTitle:@"Drop down to refresh" forState:MJRefreshStateIdle];
        [header setTitle:@"Release to refresh" forState:MJRefreshStatePulling];
        header.lastUpdatedTimeLabel.text = @"22";
    }
    
    CGFloat bottomVW = [self.env isEqualToString:@"zh"] ? 65 : 165;
    self.bottomView.frame = CGRectMake(SCREEN_WIDTH/2-bottomVW/2, 0, bottomVW, HOME_BOTTOM_H);
    
    //MARK: -- 切换语言
    self.switchLab.singalTapBlock = ^{
      
        CGFloat bottomVW = [weakSelf.env isEqualToString:@"en"] ? 65 : 165;
        weakSelf.bottomView.frame = CGRectMake(SCREEN_WIDTH/2-bottomVW/2, 0, bottomVW, HOME_BOTTOM_H);
        
        if ([weakSelf.env isEqualToString:@"en"]) {
            [weakSelf loadData:@"zh"];
            weakSelf.env = @"zh";
            weakSelf.clientImageView.frame = CGRectMake(0, 30-25/2, 25, 25);
            weakSelf.clientLab.frame = CGRectMake(30, 0, 40, 60);
            
        }else{
            [weakSelf loadData:@"en"];
            weakSelf.env = @"en";
            weakSelf.clientImageView.frame = CGRectMake(0, 30-25/2, 25, 25);
            weakSelf.clientLab.frame = CGRectMake(30, 0, 140, 60);
            
        
        }
    };
}

//MARK: -- UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.models.count == 0) {
        return 0;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.models.count == 0) {
        return 0;
    }
    return self.models.count+2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeCategoryCell class])];
    
    if (cell == NULL) {
        cell = [[HomeCategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([HomeCategoryCell class])];
    }
    
    if (indexPath.row < self.models.count) {
        cell.itemLab.text = [self.models[indexPath.row] text];
    }else if (indexPath.row == self.models.count){
        cell.itemLab.text = self.model.textjson.index.rjet;
    }else{
        cell.itemLab.text = self.model.textjson.currency.appreciate;
    }
    
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.models.count-1) {
        
        //
        IndexListModel *m = self.models[indexPath.row];
        NSRange range = [m.src rangeOfString:@"/" options:NSBackwardsSearch];
        NSString *vcStr = [[m.src substringFromIndex:range.location+1] stringByAppendingString:@"ViewController"];
        BaseDetailViewController *vc = [NSClassFromString(vcStr) new];
        vc.env = self.env;
        vc.index = indexPath.row;
        vc.model = self.model;
        vc.backText = self.model.textjson.currency.backText;
        vc.titleText = [self.models[indexPath.row] text];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row == self.models.count-1) {
        
        //MARK: -- 其他计算
        UIAlertController *vc = [UIAlertController alertControllerWithTitle:[self.env isEqualToString:@"zh"] ? @"请选择" : @"Please choose " message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *a1 = [UIAlertAction actionWithTitle:[self.env isEqualToString:@"zh"] ? @"RJET医学计算" : @"RJET Medical computing" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          
            [self getAppStoreAppVersion:@"1573336138" jump:YES];
        }];
        UIAlertAction *a3 = [UIAlertAction actionWithTitle:[self.env isEqualToString:@"zh"] ? @"取消" : @"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [vc addAction:a1];
        [vc addAction:a3];

        [self presentViewController:vc animated:YES completion:nil];
    }else if (indexPath.row == self.models.count) {
        
        if (![AlertUtils checkWxInstall:self env:self.env]) {
            return;
        }
        
        //MARK: -- 关于我们
        WXLaunchMiniProgramReq *req = [WXLaunchMiniProgramReq object];
        req.userName = @"gh_a15483e9d5ba"; //仁杰电子
//        req.userName = @"gh_abad6e836aeb"; //小浏印象
//        req.path = @"pages/my/my.html"; //仁杰电子
//        req.path = @"pages/login/login.html"; //小浏印象
        req.miniProgramType = WXMiniProgramTypeRelease;
        [WXApi sendReq:req completion:^(BOOL success) {
            NSLog(@"拉起小程序结果: %d", success);
        }];
        
    }else if (indexPath.row == self.models.count + 1){
        
        
        PayView *v = [[PayView alloc] initWithFrame:CGRectMake(20,  SCREEN_HEIGHT/2-300/2, SCREEN_WIDTH-40, 360)];
        self.payView = v;
        self.payView.env = self.env;
        
        //MARK: -- 赞赏支持
        zhPopupController *vc = [[zhPopupController alloc] initWithView:v size:CGSizeMake(v.frame.size.width, v.frame.size.height)];
        vc.maskType = zhPopupMaskTypeDarkBlur;
        vc.presentationStyle = zhPopupSlideStyleTransform;
        vc.dismissOnMaskTouched = YES;
        [vc showInView:self.view.window completion:NULL];
        
        v.weakVc = vc;
        
        v.payblock = ^(NSInteger type, CGFloat amount) {
            
//            NSLog(@"type: %ld amount:%.2f", type, amount);
            
            if (type == 1) {
                
                if (![AlertUtils checkWxInstall:self env:self.env]) {
                    return;
                }
                
                //MARK: -- 微信支付
                [[AnimatorUtil sharedInstance] show];

                AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
                manager.requestSerializer = [AFJSONRequestSerializer serializer];
                manager.responseSerializer = [AFJSONResponseSerializer serializer];
                
                NSDictionary *parameters = @{@"price":@(amount)};

                [manager POST:@"https://data3.rjetic.com:30213/rjet/pay/pay" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
                    
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    
                    [[AnimatorUtil sharedInstance] dismiss];

                    PayModel *m = [PayModel yy_modelWithJSON:responseObject];
                    
                    PayReq *payreq = [[PayReq alloc] init];
                    payreq.partnerId = m.data.partnerid;
                    payreq.prepayId = m.data.prepayid;
                    payreq.package = m.data.package;
                    payreq.nonceStr = m.data.noncestr;
                    payreq.timeStamp = m.data.timestamp;
                    payreq.sign = m.data.sign;
                    [WXApi sendReq:payreq completion:^(BOOL success) {
                        NSLog(@"微信支付结果 : %d", success);
                    }];
                    
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    
                    [[AnimatorUtil sharedInstance] dismiss];
                    
                }];
                
            }else if(type == 2){
                
                if (![AlertUtils checkZfbInstall:self env:self.env]) {
                    return;
                }
                
                //MARK: -- 支付宝支付
                [[AnimatorUtil sharedInstance] show];

                AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//                manager.requestSerializer = [AFJSONRequestSerializer serializer];
                manager.responseSerializer = [AFJSONResponseSerializer serializer];
                
                NSDictionary *parameters = @{@"price":@(amount)};

                [manager GET:@"https://data3.rjetic.com:30213/rjet/pay" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
                    
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    
                    [[AnimatorUtil sharedInstance] dismiss];

                    AliPayModel *m = [AliPayModel yy_modelWithJSON:responseObject];
                    NSLog(@"m:%@", [m yy_modelToJSONString]);
                    NSString *appScheme = @"alipay2021004112668571";
                    [[AlipaySDK defaultService] payOrder:m.data fromScheme:appScheme callback:^(NSDictionary *resultDic) {
                                            
                        NSLog(@"支付宝支付结果: %@", resultDic);
                    }];
                    
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    
                    [[AnimatorUtil sharedInstance] dismiss];

                }];
            }

        };
        

        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


//MARKK: -- UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self search];
    return YES;
}

- (void)search
{
    [self.searchTf resignFirstResponder];
    
    if (self.searchTf.text.length == 0) {
        [self loadData:self.env];
        return;
    }
    
    [self.models removeAllObjects];
    for (NSInteger i=0; i<self.model.textjson.index.list.count; i++) {
        
        IndexListModel *model = self.model.textjson.index.list[i];
        if (model.iid == 100) {
            //其他计算 总是显示
            [self.models addObject:model];
        }
        if ([model.text containsString:self.searchTf.text]) {
            
            [self.models addObject:model];
        }
    }
    
    [self.tableview reloadData];
}


//MARK: -- 获取appstore版本号
- (void)getAppStoreAppVersion:(NSString *)appstoreCode jump:(BOOL)jump
{
    [[AnimatorUtil sharedInstance] show];
    
    [[AFHTTPSessionManager manager] GET:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@", appstoreCode] parameters:@{} progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            [[AnimatorUtil sharedInstance] dismiss];

            AppVersionModel *m = [AppVersionModel yy_modelWithJSON:responseObject];
            NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"];
            
            if (m.results != NULL && m.resultCount > 0) {
                
                if (jump) {
                    
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:m.results[0].trackViewUrl]];

                }else{
                    
                    if ([localVersion compare:m.results[0].version] == NSOrderedAscending) {
                        
                        [AlertUtils showAlertMsg:[self.env isEqualToString:@"zh"] ? @"发现新版本，立即前往更新?" : @"Discover a new version, update immediately?" sureAction:^{
                            
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:m.results[0].trackViewUrl]];

                        } cancelAction:^{
                            
                            
                        } vc:self env:self.env];
                    }
                }
                
            }
            

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [[AnimatorUtil sharedInstance] dismiss];

        }];
}


//MARK: -- 上下拉刷新
- (void)refresh
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.tableview.mj_header endRefreshing];
        });
    }];
    
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.textColor = THEME_COLOE;
    header.lastUpdatedTimeLabel.textColor = THEME_COLOE;
    self.tableview.mj_header = header;
}

@end



